/*
加载界面,我们将内容,例如model的处理我们放到子线程去运行
//还有就是打印的东西太多了

//剩下的就是处理时间

*/

import UIKit

let collectionCellID = "pictureViewCell"

//设置每一个条目之间的距离
let CZPictureViewItemMargin: CGFloat = 5

//(SCREENW - 2 * CZHomeCellMargin - 2 * CZPictureViewItemMargin)/3
let CZPictureViewItenWH: CGFloat = {
    
   let intW = Int((SCREENW - 2 * CZHomeCellMargin - 2 * CZPictureViewItemMargin)/3)
    
    //转成float
    
    let floatW = CGFloat(intW)
    
    return floatW
}()

class CZPictureView: UICollectionView {

    
    
    //定义一个属性,元祖的类型,元祖类型是多个参数,可以返回多个值
    
    //注意这里最好写可选值,这样改动的地方相对的少
    //元祖多个类型的值,进而达到将view里面显示的数据都有的状态
    var dataInfo: (pic_urls: [CZPictureModel], size: CGSize)? {
        
        
        didSet{
            
            //判断.设置itemSize
            //注意这里的dataInfo是没有智能提示的,智能我们自己手写
            if dataInfo?.pic_urls.count == 1{
                
                let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
                
                layout.itemSize = dataInfo?.size ?? CGSizeZero
                
            }else{
                
                let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
                
                layout.itemSize = CGSize(width: CZPictureViewItenWH, height: CZPictureViewItenWH)
                
                
                
            }
            
            //设置本省的view的越是
            
            self.snp_updateConstraints(closure: { (make) -> Void in
                
                make.size.equalTo(dataInfo?.size ?? CGSizeZero)
                
                })
            
            
            reloadData()
            
        }
        
        
        
        
        
        
    }
        
        //当前微博的视图的数据,因为这是一个collectionViewCell,所以需要数组,来设置数据源的方法
//        var pic_urls: [CZPictureModel]?{
//            
//            didSet{
//                
//                //赋值数据,刷新界面`
//                //给label进行赋值,这里注意可选和必选值的设置
//                label.text = "\(pic_urls?.count ?? 0)"
//                //刷新数据
//                
//                self.reloadData()
//                
//            }
//            
//        }
        //这个多图的view我们是用collectionView来进行设置
        
        override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
            
           
            super.init(frame: frame, collectionViewLayout: layout)
            setupUI()
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        private func setupUI(){
            
            
            
            
            self.backgroundColor = UIColor.orangeColor()
            //注册cell
            self.registerClass(CZPictureViewCell.self, forCellWithReuseIdentifier: collectionCellID)
            
            //设置每一个item的大小
            //注意这里需要获取,还有就是进行强转
            let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
            
            
            layout.itemSize = CGSizeMake(CZPictureViewItenWH, CZPictureViewItenWH)
            layout.minimumInteritemSpacing = CZPictureViewItemMargin
            layout.minimumLineSpacing = CZPictureViewItemMargin
            
            //设置数据源
            self.dataSource = self
            addSubview(label)
            
            
            //设置约束
            label.snp_makeConstraints { (make) -> Void in
                
                make.center.equalTo(self)
            }
            
        }
        //添加控件,显示里面有几个图片
        private lazy var label: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 35)
        
        
        
        
        
        
    }
    
    
extension CZPictureView: UICollectionViewDataSource{
        
        //注册cell
        
        func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            
            return dataInfo?.pic_urls.count ?? 0
        }
        
        func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
            
            let cell = collectionView.dequeueReusableCellWithReuseIdentifier(collectionCellID, forIndexPath: indexPath) as! CZPictureViewCell
            cell.backgroundColor = RandomColor
            
            //为cell里面的数据赋值
            cell.pictureModel = dataInfo!.pic_urls[indexPath.item]
            
            return cell
            
        }
        
        
        
        
        
    }
    
    //自定义配图的cell
    
    class CZPictureViewCell: UICollectionViewCell {
        
        //cell里面有一个属性,进行设置,提供给外界设置图片的属性
        var pictureModel: CZPictureModel?{
            
            didSet{
                //为里面的属性进行赋值
                imageView.sd_setImageWithURL(NSURL(string: pictureModel?.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "timeline_image_placeholder"))
                //判断这个thumbnail_pic是否是以gif结尾的
                
                //            let d = pictureModel?.thumbnail_pic?.hasSuffix(".gif")
                //注意suffix
                //            if let d = pictureModel?.thumbnail_pic where d.hasSuffix(".gif") {
                //
                //                gifImageView.hidden = false
                //
                //            }else{
                //                gifImageView.hidden = true
                //            }
                
                //直接三目运算符,这样更加的简单,这里转换的有点繁琐
                
                gifImageView.hidden = !(pictureModel?.thumbnail_pic ?? "").hasSuffix(".gif")
                
                
                
            }
            
            
            
        }
        
        override init(frame: CGRect) {
            super.init(frame: frame)
            setupUI()
            
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        private func setupUI(){
            
            contentView.addSubview(imageView)
            contentView.addSubview(gifImageView)
            //2.添加约束
            imageView.snp_makeConstraints { (make) -> Void in
                
                make.edges.equalTo(contentView)
            }
            gifImageView.snp_makeConstraints { (make) -> Void in
                
                make.bottom.trailing.equalTo(contentView)
            }
            
            
        }
        
        //懒加载控件
        //imageview,设置其内部的属性
        private lazy var imageView: UIImageView = {
            
            let imgView = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
            //设置内容的模式 fill 自适应的装满(切图), fit(完整显示)
            imgView.contentMode = .ScaleAspectFill
            //裁剪超出部分
            imgView.clipsToBounds = true
            
            
            return imgView
        }()
        //添加gif的图片
        private lazy var gifImageView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
        
        
        
        
    }



